package cn.bgs.service.impl;

import cn.bgs.dao.EmpMapper;
import cn.bgs.dao.EmployeeInfoMapper;
import cn.bgs.dao.EmployeeRegisterMapper;
import cn.bgs.pojo.EmployeeInfo;
import cn.bgs.pojo.EmployeeRegister;
import cn.bgs.service.EmpService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired private EmpMapper empMapper;
    @Autowired private EmployeeRegisterMapper employeeRegisterMapper;
    @Autowired private EmployeeInfoMapper employeeInfoMapper;

    @Override
    public PageInfo<EmployeeInfo> findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
         List<EmployeeInfo> list= empMapper.findAll();

         PageInfo<EmployeeInfo> p=new PageInfo<>(list);
            return p;

    }

    @Override
    public EmployeeRegister login(String email, String password) {
        return empMapper.login(email,password);
    }

    @Override
    public void add(EmployeeInfo employeeInfo) {
        empMapper.add(employeeInfo);
    }



    @Override
    public void delete(Integer pkId) {
        empMapper.delete(pkId);
    }

    @Override
    public void register(String username, String email, String password) {

        String employeeId = UUID.randomUUID().toString().replaceAll("-","");


        EmployeeRegister employeeRegister= new EmployeeRegister();

        employeeRegister.setEmployeeId(employeeId);
        employeeRegister.setEmail(email);
        employeeRegister.setPassword(password);
        employeeRegisterMapper.insertSelective(employeeRegister);

        EmployeeInfo employeeInfo =new EmployeeInfo();
        employeeInfo.setEmployeeId(employeeId);
        employeeInfo.setEmployeeName(username);
        employeeInfo.setEmail(email);
        employeeInfoMapper.insertSelective(employeeInfo);

    }

}
